package sg.edu.nus.iss.billing.service.account;

import java.util.ArrayList;
import java.util.List;

import sg.edu.nus.iss.billing.common.Util;
import sg.edu.nus.iss.billing.common.exception.BadDaoCallException;
import sg.edu.nus.iss.billing.common.exception.BadDbCallException;
import sg.edu.nus.iss.billing.dao.account.AccountDAO;
import sg.edu.nus.iss.billing.service.subscription.ISubscription;
import sg.edu.nus.iss.billing.service.subscription.SubscriptionImpl;
import sg.edu.nus.iss.billing.vo.account.AccountVO;
import sg.edu.nus.iss.billing.vo.subscription.CableTvVO;
import sg.edu.nus.iss.billing.vo.subscription.DigitalVoiceVO;
import sg.edu.nus.iss.billing.vo.subscription.MobileVoiceVO;
import sg.edu.nus.iss.billing.vo.subscription.SubscriptionVO;
import sg.edu.nus.iss.billing.vo.subscription.feature.ChannelFeatureVO;

public class AccountImpl {
	
	private AccountVO accountVO;
	private AccountDAO accountDao;
	private ISubscription subscriptionImpl;
	
	private String logImpl = "CLASSNAME : AccountImpl :  ";
	
	private void printLog(String str){
		System.out.println(str);
	}
	
	public AccountImpl(){
		printLog(logImpl + "CONSTRUCTOR CALLED ");
		accountDao = new AccountDAO();
		printLog(logImpl + "CONSTRUCTOR CALL OVER");
	}
	
	
	public void createAccount(AccountVO accountVO ) throws BadDaoCallException, Exception{
		//accountVO.setAccountNo("ACCT-"+Util.generateRandomNumer());
		String logImpl1 = logImpl + "createAccount() Account Number : "+accountVO.getAccountNo() + " : ";
		printLog(logImpl1 + "Setting STATUS active");
		accountVO.setStatus(Util.SUBSCRIPTION_STATUS_ACTIVE);
		printLog(logImpl1 + "Setting calling  accountDao.createAccount");
		accountDao.createAccount(accountVO);
		printLog(logImpl1 + "method call over successfully");
		
	}
	
	public void createSubscriptions(  ArrayList<SubscriptionVO> listSubscriptionVO ){
		String logImpl1 = logImpl +"createSubscriptions() : size of subscription "+listSubscriptionVO.size() + " : ";
		subscriptionImpl = new SubscriptionImpl(accountVO.getAccountNo());
		subscriptionImpl.createSubscriptions(listSubscriptionVO);
		
	}


	public void modifyAccount(AccountVO accountVO) throws BadDbCallException {
		String logImpl1 = logImpl + "modifyAccount() : Account In Context  "+accountVO.getAccountNo() + " : ";
		printLog(logImpl1 + "accountVO.getStatus() : "+accountVO.getStatus());
		if(accountVO.getStatus()!=null && accountVO.getStatus().equals(Util.ACCOUNT_STATUS_INACTIVE)){
			printLog(logImpl1 + "ACCOUNT IS INACTIVE");
			accountDao.updateAcctStatus(accountVO.getAccountNo(), accountVO.getNric(), accountVO.getStatus());
			printLog(logImpl1 + " STATUS UPDTED TO INACTIVE FOR ACCOUNT");
			return;
		}
		printLog(logImpl1 + " ACCOUNT IS ACTIVE");
		subscriptionImpl = new SubscriptionImpl(accountVO.getAccountNo());
		printLog(logImpl1 + " MODIFY SUBSCRIPTION");
		subscriptionImpl.modifySubscription(accountVO.getListSubscriptionVO());
		printLog(logImpl1 + " MODIFY SUBSCRIPTION COMPLETE");

		
	}


	public AccountVO getAccountDetailsByAccountNumber(String accountNumber) {
		String logImpl1 = logImpl + "getAccountDetailsByAccountNumber() : Account In Context  "+ accountNumber + " : ";
		printLog(logImpl1 + "CALLING ACCOUNTDAO");
		accountVO =  accountDao.getAccount(accountNumber);
		if(accountVO!=null){
		printLog(logImpl1 + "accountVO getNric "+accountVO.getNric());
		printLog(logImpl1 + "accountVO  getStatus "+accountVO.getStatus());
		}
		
		if(accountVO!=null){
			subscriptionImpl = new SubscriptionImpl(accountVO.getAccountNo());
			List<SubscriptionVO> subslist = subscriptionImpl.getAllSubscriptionsForAcct();
			printLog(logImpl1 + "CALLING subscriptionImpl.getAllSubscriptionsForAcct: size : "+subslist.size());
			accountVO.setListSubscriptionVO(subslist);
			
			   		 for (int i =0; i<subslist.size();i++){
			   			printLog(logImpl1 + "subslist.get(i) = " + i + " "+ subslist.get(i).getClass());
				 
					 if(subslist.get(i) instanceof CableTvVO){
						 CableTvVO  cableTvVO = (CableTvVO) subslist.get(i);
						 System.out.println(logImpl1 + "  : CableTvVO :  SubsId: "+ cableTvVO.getSubsId());
						 System.out.println(logImpl1 + "  : CableTvVO :  Acct No: "+ cableTvVO.getAccNo());
						 
						 if(cableTvVO.getAllAddonChannel()!=null){
						 					List	<ChannelFeatureVO>	 featureVoList = cableTvVO.getAllAddonChannel();
							 for (int j=0; j<featureVoList.size(); j++) {					
								 System.out.println(logImpl1 +" CableTvVO  ChannelFeatureVO : "+ featureVoList.get(j).getName() + " "+featureVoList.get(j).getDescription() );

							}
						 }
								 
					 }
					 else  if(subslist.get(i) instanceof DigitalVoiceVO){
					 		DigitalVoiceVO digitalVoiceVO = (DigitalVoiceVO) subslist.get(i);
							
								System.out.println(logImpl1 + "  : digitalVoiceVO : "+ digitalVoiceVO.getAccNo());
								System.out.println(logImpl1 + "  : digitalVoiceVO : "+digitalVoiceVO.getSubsId());
								System.out.println(logImpl1 + "  : digitalVoiceVO : "+digitalVoiceVO.getPhoneNumber());
								System.out.println(logImpl1 + "  : digitalVoiceVO : "+digitalVoiceVO.getStatus());
								System.out.println(logImpl1 + "  : digitalVoiceVO : "+digitalVoiceVO.getStartDate());
								System.out.println(logImpl1 + "  : digitalVoiceVO : "+digitalVoiceVO.getEndDate());	
								
								for(int j=0;j<	digitalVoiceVO.getAllOptionalFeature().size();j++){
									System.out.println(logImpl1 + "  : digitalVoiceVO : "+digitalVoiceVO.getAllOptionalFeature().get(j).getName());	
									System.out.println(logImpl1 + "  : digitalVoiceVO : "+digitalVoiceVO.getAllOptionalFeature().get(j).getDescription());	
								}
					 	 }
					 	 
					 else  if(subslist.get(i) instanceof MobileVoiceVO){
					 		MobileVoiceVO mobileVoiceVO = (MobileVoiceVO) subslist.get(i);
									 
										System.out.println(logImpl1 + "  : mobileVoiceVO : "+ mobileVoiceVO.getAccNo());
										System.out.println(logImpl1 + "  : mobileVoiceVO : "+mobileVoiceVO.getSubsId());
										System.out.println(logImpl1 + "  : mobileVoiceVO : "+mobileVoiceVO.getPhoneNumber());
										System.out.println(logImpl1 + "  : mobileVoiceVO : "+mobileVoiceVO.getStatus());
										System.out.println(logImpl1 + "  : mobileVoiceVO : "+mobileVoiceVO.getStartDate());
										System.out.println(logImpl1 + "  : mobileVoiceVO : "+mobileVoiceVO.getEndDate());	
										for(int j=0;j<	mobileVoiceVO.getAllOptionalFeature().size();j++){
											System.out.println(logImpl1 + "  : digitalVoiceVO : "+mobileVoiceVO.getAllOptionalFeature().get(j).getName());	
											System.out.println(logImpl1 + "  : digitalVoiceVO : "+mobileVoiceVO.getAllOptionalFeature().get(j).getDescription());	
										}
		}
			   		 }
	}
			   		 printLog(logImpl1 + "CALLING SEtting list in the AccountVO");
			   		return accountVO;
		}

	public AccountVO getAccountDetailsByNric(String nric) {
		accountVO =  accountDao.getAccount(nric);	
		if(accountVO!=null){
			subscriptionImpl = new SubscriptionImpl(accountVO.getAccountNo());
			List<SubscriptionVO> subslist = subscriptionImpl.getAllSubscriptionsForAcct();
			accountVO.setListSubscriptionVO(subslist);
		}
		return accountVO;
	}

	public void deregisterAccount(String nric, AccountVO acccountVO, String status) throws BadDbCallException{
		
		AccountVO avo = new AccountVO();
		if(accountVO==null){
			avo = accountDao.getAcctByNric(nric.trim());
		}else{
			avo = accountVO;
		}
		String accountNo = avo.getAccountNo();
		accountDao.updateAcctStatus(accountNo, nric, status);
	}

	
}
